import { createStore } from 'vuex';

//引入菜单管理的Vuex 
import menu from './modules/menu';
//引入角色管理的Vuex
import role from './modules/role';
//引入管理员管理的Vuex
import user from './modules/user';



//引入登录接口
import { login } from "../request/api";
//引入弹窗提示
import { ElMessage } from 'element-plus';

let data: any = sessionStorage.getItem('userInfo')
export default createStore({
  state: {
    userInfo: JSON.parse(data)
  },
  getters: {
    getuserInfo(state) {
      return state.userInfo
    }
  },
  mutations: {
    REQ_USERINFO(state, payload) {
      //Vuex刷新会消失数据  借助储存解决
      if (payload) {
        // 手动赋值  
        state.userInfo = payload
        sessionStorage.setItem('userInfo', JSON.stringify(payload));
      } else {
        sessionStorage.removeItem('userInfo');
        // 手动清空
        state.userInfo = []
      }
    }
  },
  actions: {
    //调用登录接口
    async userLogin({ commit }: any, payload: any) {

      let res: any = await login(payload);
      //登录成功
      if (res.code == 200) {
        //提交数据
        commit('REQ_USERINFO', res.list);
        //显示登录成功
        ElMessage({
          message: "登录成功",
          type: "success",
          duration: 1000
        });
      } else {
        ElMessage.error(res.msg);
      }
    }

  },
  modules: {
    menu, role, user
  }
})
